<?php
// +----------------------------------------------------------------------
// | CRMEB [ CRMEB赋能开发者，助力企业发展 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2016~2023 https://www.crmeb.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed CRMEB并不是自由软件，未经许可不能去掉CRMEB相关版权
// +----------------------------------------------------------------------
// | Author: CRMEB Team <admin@crmeb.com>
// +----------------------------------------------------------------------
namespace app\adminapi\validate\marketing;

use think\Validate;

class StoreFreeValidate extends Validate
{

    /**
     * 定义验证规则
     * 格式：'字段名'    =>    ['规则1','规则2'...]
     *
     * @var array
     */
    protected $rule = [
        'title' => 'require',
        'start_time' => 'require|integer',
        'end_time' => 'require|integer|gt:start_time',
        'add_marketing_type' => 'require|in:0,1,2,3',
        'is_use_all_product' => 'require|in:0,1',
        'productRelations' => 'array|checkProductRelationsRules',
        'rules' => 'array|checkRulesRules',
        'type'  =>  'require|in:1,2'
    ];

    /**
     * 定义错误信息
     * 格式：'字段名.规则名'    =>    '错误信息'
     *
     * @var array
     */
    protected $message = [
        'title.require' => '30045', // 活动名称不能为空
        'start_time.require' => '30046', // 开始时间不能为空
        'start_time.integer' => '30047', // 开始时间必须是整数
        'end_time.require' => '30048', // 结束时间不能为空
        'end_time.integer' => '30049', // 结束时间必须是整数
        'end_time.gt' => '30050', // 结束时间必须大于开始时间
        'add_marketing_type.require' => '30051', // 营销类型不能为空
        'add_marketing_type.in' => '30052', // 营销类型值非法
        'is_use_all_product.require' => '30053', // 是否适用所有商品不能为空
        'is_use_all_product.in' => '30054', // 是否适用所有商品值非法
        'productRelations.array' => '30055', // 适用商品必须是数组
        'rules.array' => '30056', // 所属规则必须是数组
        'type.require'  =>  '30060', // 缺少满额或者满赠选择
        'type.in'   =>  '30061', // 满额或者满赠参数异常
    ];


    protected $scene = [
        'save' => ['title', 'start_time', 'end_time', 'add_marketing_type', 'is_use_all_product', 'productRelations', 'rules', 'type'],
    ];

    protected function checkProductRelationsRules($value, $rule, $data)
    {
        if (!empty($value)) {
            foreach ($value as $product) {
                if (!isset($product['product_id']) || !isset($product['product_attr_value_unique'])) {
                    return '30057'; // 商品规则错误
                }
            }
        }
        return true;
    }

    protected function checkRulesRules($value, $rule, $data)
    {
        if (!empty($value)) {
            foreach ($value as $rule) {
                if (!isset($rule['min_price']) || !isset($rule['nums']) || !isset($rule['products']) || !isset($rule['type']) || !isset($rule['min_count'])) {
                    return '30058'; // 规则规则错误
                }
                foreach ($rule['products'] as $product) {
                    if (!isset($product['product_id']) || !isset($product['product_attr_value_unique']) || !isset($product['give_count']) || !isset($product['activity_count'])) {
                        return '30059'; // 规则中的商品规则错误
                    }
                }
            }
        }
        return true;
    }

}
